<?php
class Merchant_Model_AlertMessages
{

	private $_table;
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_AlertMessages();
	}


	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='am_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}
	
	public  function updateByCond($row,$cond){
		$db = $this->_table->getAdapter();
		$where = '1';
		foreach ($cond as $key =>$val){
			$where .= $db->quoteInto(" and {$key} = ?",$val);
		}
		return $this->_table->update($row, $where);
	}

	public function delete($value,$field='am_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);
		return $this->_table->delete($where);
	}


	public function getByValue($value,$field='am_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
	
	public function getColumnByValue($col,$value,$field){
         $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,$col);
        $select->where("{$field} = ?",$value);
        $select->order('am_id asc');
		return $this->_table->getAdapter()->fetchAll($select);
	}

	/**
	 *  $cond 数组，字段为索引；$type 判断查询字段
	 *  $order 排序方式；$mode，'row' 返回一行数据，'all'返回所有数据
	 */
	public function getRowByCond($cond,$type,$order='',$mode){
		$table = $this->_table->info('name');
		if($order==''){
			$order='am_id asc';
		}
		$select = $this->_table->select()->from($table, $type);
		if(isset($cond['am_config_id']) && $cond['am_config_id'] !== ''){
			$select->where(' am_config_id = ?',$cond['am_config_id']);
		}
		if(isset($cond['customer_id']) && $cond['customer_id'] !== ''){
			if(is_array($cond['customer_id'])){
				$select->Where(' customer_id IN(?)', $cond['customer_id']);
			}else{
				$select->where(' customer_id = ?',$cond['customer_id']);
			}
		}		

		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        $select->order($order);

        if($mode=='row'){
        	return $this->_table->fetchRow($select)->toArray();
        }else {
        	return $this->_table->fetchAll($select)->toArray();
        }
		
	}
	
 
}